package net.lainiao.morethread.Chapter4.demo9;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/3.
 */
public class Main {
    public static void main(String[] args) {
        ExecutorService executor=(ExecutorService) Executors.newCachedThreadPool();
        ResultTask resultTask[]=new ResultTask[5];
        for (int i = 0; i < 5; i++) {
            ExecutableTask executableTask=new ExecutableTask("Task"+i);
            resultTask[i]=new ResultTask(executableTask);
            executor.submit(resultTask[i]);
        }

        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < resultTask.length; i++) {
            resultTask[i].cancel(true);
        }

        for (int i = 0; i < resultTask.length; i++) {
            if(!resultTask[i].isCancelled()){
                try {
                    System.out.println(resultTask[i].get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }

        executor.shutdown();

    }
}
